var role = {
    data: {
        pageData: {
            count: 0,
            pageNum: 1,
            pageSize: 10
        },
        functionIds: []
    },
    start: function () {
        this.onLoad()
        this.bindEvent()
    },
    onLoad: function () {
        this.loadTableData()
    },
    bindEvent: function () {

        var _this = this

        $('#role_search_btn').click(function () {
            _this.data.pageData.count = 0
            _this.data.pageData.pageNum = 1
            _this.loadTableData()
        })

        $('#role_name_search_text').keyup(function (e) {
            if (e.keyCode == 13) {
                _this.data.pageData.count = 0
                _this.data.pageData.pageNum = 1
                _this.loadTableData()
            }
        })

        $('#reset_search_btn').click(function () {
            $('#role_name_search_text').val('')
        })

        $('#add_role_btn').click(function () {

            sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/function/getAllFunctions', 'post', {}, function (res) {

                if (res.status == 0) {

                    $('#function_content').append(_this.generateFunctions(res.data))

                    layui.use('form', function () {
                        layui.form.render()
                    })

                    layer.open({
                        type: 1,
                        content: $('#role_form_content'),
                        area: ['800px', '500px'],
                        btn: ['提交', '取消'],
                        btnAlign: 'c',
                        shadeClose: true,
                        maxmin: true,
                        success: function (layero, index) {
                        },
                        yes: function (index, layero) {
                            if (_this.validateForm()) {

                                _this.data.index = sinoUtil.loading()

                                _this.data.functionIds = []
                                $.each($('#function_content input:checkbox:checked'), function () {
                                    _this.data.functionIds.push($.trim($(this).val()));
                                })

                                sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/role/addRoleInfo', 'post', {
                                    roleName: $.trim($('#role_name').val()),
                                    remark: $.trim($('#remark').val()),
                                    functionIds: _this.data.functionIds.join(',')
                                }, function (res) {
                                    if (res.status == 0) {
                                        layer.close(index)
                                        sinoUtil.stopLoadingAndShowResult(_this.data.index, 1, '新增成功')
                                        _this.loadTableData()
                                    } else if (res.status == 10 || res.status == 11) {
                                        window.parent.location.href = '/system/login'
                                    } else {
                                        sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, res.msg)
                                    }
                                }, function (error) {
                                    layer.close(index)
                                    sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, '新增失败')
                                }, false)
                            }
                        },
                        end: function () {
                            $('#role_name').val('')
                            $('#remark').val('')
                            $('#function_content').empty()
                        }
                    })
                } else if (res.status == 10 || res.status == 11) {
                    window.parent.location.href = '/system/login'
                }
            }, function (error) {
                layer.msg('拉取权限信息失败')
            }, false)

        })

    },
    loadTableData: function () {

        var _this = this

        sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/role/queryRoles', 'get', {
            roleName: $.trim($('#role_name_search_text').val()),
            pageNum: _this.data.pageData.pageNum,
            pageSize: _this.data.pageData.pageSize
        }, function (res) {
            if (res.status == 0) {
                layui.use('table', function () {

                    var table = layui.table

                    table.render({
                        elem: '#role_table'
                        , height: 352
                        , url: ''
                        , data: res.data.list
                        , page: false
                        , cols: [[
                            {
                                field: 'roleName',
                                title: '角色名称',
                                width: '30%',
                                minWidth: 100,
                                align: 'center'
                            },
                            {
                                field: 'remark',
                                title: '备注',
                                width: '50%',
                                minWidth: 150,
                                align: 'center'
                            },
                            {
                                title: '操作',
                                fixed: 'right',
                                width: '20%',
                                align: 'center',
                                minWidth: 100,
                                toolbar: '#role_table_tool_bar'
                            }
                        ]]
                        , text: {
                            none: '暂无相关数据'
                        }
                    })

                    table.on('tool(role)', function (obj) {

                        var layEvent = obj.event;

                        if (layEvent === 'editRoleInfo') {
                            _this.data.rowData = obj.data
                            _this.editRowData()
                        }
                        else if (layEvent === 'delRoleInfo') {
                            _this.data.rowData = obj.data
                            _this.delRowData()
                        }

                    })
                })

                _this.data.pageData.count = res.data.total

                _this.loadPageContent()

            } else if (res.status == 11 || res.status == 10) {
                window.parent.location.href = '/system/login'
            } else {
                _this.data.pageData.count = 0
                _this.data.pageData.pageNum = 1
                layer.msg(res.msg)
            }
        }, function (error) {
            _this.data.pageData.count = 0
            _this.data.pageData.pageNum = 1
            layer.msg('哪里出错了')
        }, false)

    },
    loadPageContent: function () {

        var _this = this

        layui.use('laypage', function () {

            var laypage = layui.laypage

            laypage.render({
                elem: 'role_page_content'
                , count: _this.data.pageData.count
                , limit: _this.data.pageData.pageSize
                , curr: _this.data.pageData.pageNum
                , jump: function (obj, first) {
                    if (!first) {
                        _this.data.pageData.pageNum = obj.curr
                        _this.data.pageData.pageSize = obj.limit
                        _this.loadTableData()
                    }
                }
            })
        })

    },
    delRowData: function () {

        var _this = this

        layer.confirm('数据删除后将不可恢复,确定要删除吗?', function (index) {
            layer.close(index)
            _this.data.index = sinoUtil.loading()
            sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/role/deleteRoleInfo', 'post', {
                roleIds: _this.data.rowData.roleId
            }, function (res) {
                if (res.status == 0) {
                    sinoUtil.stopLoadingAndShowResult(_this.data.index, 1, '删除成功')
                    _this.data.pageData.count = 0
                    _this.data.pageData.pageNum = 1
                    _this.loadTableData()
                } else if (res.status == 10 || res.status == 11) {
                    window.parent.location.href = '/system/login'
                } else {
                    sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, res.msg)
                }
            }, function (error) {
                sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, '删除失败')
            }, false)
        })
    },
    editRowData: function () {

        var _this = this

        sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/function/getAllFunctions', 'post', {}, function (res) {

            if (res.status == 0) {

                $('#function_content').append(_this.generateFunctions(res.data, _this.data.rowData.roleFunctionIds))

                $('#role_name').val(_this.data.rowData.roleName)
                $('#remark').val(_this.data.rowData.remark)

                layui.use('form', function () {
                    layui.form.render()
                })

                layer.open({
                    type: 1,
                    content: $('#role_form_content'),
                    area: ['800px', '500px'],
                    btn: ['提交', '取消'],
                    btnAlign: 'c',
                    shadeClose: true,
                    maxmin: true,
                    success: function (layero, index) {
                    },
                    yes: function (index, layero) {
                        if (_this.validateForm()) {

                            _this.data.index = sinoUtil.loading()

                            _this.data.functionIds = []
                            $.each($('#function_content input:checkbox:checked'), function () {
                                _this.data.functionIds.push($.trim($(this).val()));
                            })

                            sinoUtil.sendHttp(sinoUtil.httpPrefix + '/api/role/editRoleInfo', 'post', {
                                roleId: _this.data.rowData.roleId,
                                roleName: $.trim($('#role_name').val()),
                                remark: $.trim($('#remark').val()),
                                functionIds: _this.data.functionIds.join(',')
                            }, function (res) {
                                if (res.status == 0) {
                                    layer.close(index)
                                    sinoUtil.stopLoadingAndShowResult(_this.data.index, 1, '修改成功')
                                    _this.loadTableData()
                                } else if (res.status == 10 || res.status == 11) {
                                    window.parent.location.href = '/system/login'
                                } else {
                                    sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, res.msg)
                                }
                            }, function (error) {
                                layer.close(index)
                                sinoUtil.stopLoadingAndShowResult(_this.data.index, 2, '修改失败')
                            }, false)
                        }
                    },
                    end: function () {
                        $('#role_name').val('')
                        $('#remark').val('')
                        $('#function_content').empty()
                    }
                })
            } else if (res.status == 10 || res.status == 11) {
                window.parent.location.href = '/system/login'
            }
        }, function (error) {
            layer.msg('拉取权限信息失败')
        }, false)
    },
    generateFunctions: function (funcArr, checkedArr) {

        var funcStr = ''
        var flag = false

        for (var i = 0, iLength = funcArr.length; i < iLength; i++) {

            if (checkedArr) {
                for (var j = 0, jLength = checkedArr.length; j < jLength; j++) {
                    if (funcArr[i].functionId == checkedArr[j]) {
                        flag = true
                        break
                    }
                }
                if (flag) {
                    funcStr += '<input type="checkbox" value="' + funcArr[i].functionId + '" title="' + funcArr[i].functionName + '" checked>'
                } else {
                    funcStr += '<input type="checkbox" value="' + funcArr[i].functionId + '" title="' + funcArr[i].functionName + '">'
                }
                flag = false
            } else {
                funcStr += '<input type="checkbox" value="' + funcArr[i].functionId + '" title="' + funcArr[i].functionName + '">'
            }
        }
        return funcStr
    },
    validateForm: function () {

        if (!sinoUtil.validate($('#role_name').val(), 'require')) {
            sinoUtil.validateAndChangeClass('#role_name', false, "请填写角色名称")
            return false
        } else {
            sinoUtil.validateAndChangeClass('#role_name', true, "校验通过")
        }

        return true

    }
}

$(function () {
    role.start()
})